package com.hhd.flow.engine.cmd;

import com.alibaba.fastjson.JSONObject;
import com.hhd.flow.engine.cmd.impl.virtual.VirtualContext;
import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.engine.variable.FlowVariablesContainer;
import com.hhd.flow.engine.vo.FlowModel;
import com.hhd.flow.engine.vo.UserManualSelect;
import com.hhd.flow.entity.FlowPath;
import com.hhd.flow.entity.FlowProcessDefinitionRef;
import com.hhd.flow.entity.FlowProcessInstance;
import com.hhd.flow.entity.FlowTask;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.apache.logging.log4j.util.Strings;

import java.util.HashSet;
import java.util.LinkedHashSet;

@Builder
@Getter
@Setter
public class PassTaskContext {
    private String formTypeId;
    private String formId;
    private String flowTaskId;
    private String flowProcessInstanceId;
    private UserManualSelect userManualSelect; //手动指定的审批人
    private JSONObject formModel;
    private FlowVariablesContainer variablesContainer;

    //只有上面三个字段是需要提前设置的
    private FlowModel flowModel;
    private FlowTask flowTask;
    private FlowProcessInstance flowProcessInstance;
    private FlowProcessDefinitionRef flowProcessDefinitionRef;
    FlowPath path;
    VirtualContext virtualContext;

    public FlowPath addPath(String... ids) {
        HashSet<String> set = new LinkedHashSet<>();
        if (!CommonUtils.isEmpty(path.getPath())) {
            for (String path : path.getPath().split(",")) {
                set.add(path);
            }
        }

        for (String id : ids) {
            if (CommonUtils.isEmpty(id)) continue;
            set.add(id);
        }
        path.setPath(Strings.join(set, ','));
        return path;
    }
}